package com.hcb.micro.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.CashRed;
import com.hcb.micro.dto.CashRedCategory;
import com.hcb.micro.dto.CashRedMall;
import com.hcb.micro.dto.CashRedProduct;
import com.hcb.micro.dto.CashRedSeller;
import com.hcb.micro.dto.Category;
import com.hcb.micro.dto.Mall;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.Product;
import com.hcb.micro.dto.ProductSeller;
import com.hcb.micro.service.ICashRedCategoryService;
import com.hcb.micro.service.ICashRedLogService;
import com.hcb.micro.service.ICashRedMallService;
import com.hcb.micro.service.ICashRedProductService;
import com.hcb.micro.service.ICashRedSellerService;
import com.hcb.micro.service.ICashRedService;
import com.hcb.micro.service.ICategoryMallService;
import com.hcb.micro.service.ICategoryService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductSellerService;
import com.hcb.micro.service.IProductService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

@Controller
public class CashRedController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	ICashRedService cashRedService;
	
	@Autowired
	ICashRedLogService cashRedLogService;
	
	@Autowired
	ICashRedMallService cashRedMallService;
	
	@Autowired
	ICashRedCategoryService cashRedCategoryService;
	
	@Autowired
	ICashRedSellerService cashRedSellerService;
	
	@Autowired
	ICashRedProductService cashRedProductService;
	
	@Autowired
	IProductService productService;
	
	@Autowired
	IProductSellerService productSellerService;
	
	@Autowired
	ICategoryService categoryService;
	
	@Autowired
	ICategoryMallService categoryMallService;
	
	@RequestMapping(value = "cashRed/list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("cashRedId") != null && !"".equals(bodyInfo.get("cashRedId"))) {
			para.put("cashRedId", bodyInfo.getString("cashRedId"));
		}
		if (bodyInfo.get("isActive") != null && !"".equals(bodyInfo.get("isActive"))) {
			para.put("isActive", (byte)1);
		}
		List<CashRed> cashRedAllList = cashRedService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<CashRed> cashRedList = cashRedService.selectByPara(para);
		
		if(cashRedList.size() <= 0){
			json.put("result", "1");
			json.put("description", "红包列表不存在");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String,Object>> list = new ArrayList<>();
		for(CashRed cashRed:cashRedList){
			Map<String,Object> map = new HashMap<>();
			
			map.put("id", cashRed.getId()+"");
			map.put("name", cashRed.getName()==null?"":cashRed.getName());
			map.put("conditionCash", cashRed.getConditionCash());
			map.put("rewardCash", cashRed.getRewardCash());
			map.put("isActive", cashRed.getIsActive());
			String type = "";
			switch(cashRed.getType()){
			case 1:
				type = "通用";
				break;
			case 2:
				type = "专用";
				break;
			case 3:
				type = "注册红包";
				break;
			case 4:
				type = "节日红包";
				break;
			}
			
			map.put("type", type);
			map.put("useCashCondition", cashRed.getUseCash()==null?"":cashRed.getUseCash().split(",")[0]);
			map.put("useCashResult", cashRed.getUseCash()==null?"":cashRed.getUseCash().split(",")[1]);
			map.put("useCash", cashRed.getUseCash()==null?"":"满"+cashRed.getUseCash().split(",")[0]+"可使用"+cashRed.getUseCash().split(",")[1]);
			map.put("startTime", cashRed.getStartTime()==null?"":sdf.format(cashRed.getStartTime()));
			map.put("endingTime", cashRed.getEndingTime()==null?"":sdf.format(cashRed.getEndingTime()));
			map.put("createTime", cashRed.getCreateTime()==null?"":sdf.format(cashRed.getCreateTime()));
			map.put("createTime", cashRed.getCreateTime()==null?"":sdf.format(cashRed.getCreateTime()));
			map.put("productId", cashRed.getProductId()==null?"":cashRed.getProductId());
			map.put("limitCount", cashRed.getLimitCount()==null?"":cashRed.getLimitCount());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取红包列表成功");
		json.put("list", list);
		json.put("count", cashRedAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "cashRed/detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("cashRedId") == null || "".equals(bodyInfo.get("cashRedId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CashRed cashRed = cashRedService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("cashRedId")));
		
		if(cashRed == null){
			json.put("result", "1");
			json.put("description", "红包信息不存在");
			return buildReqJsonObject(json);
		}
		Map<String,Object> map = new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		map.put("id", cashRed.getId()+"");
		map.put("name", cashRed.getName()==null?"":cashRed.getName());
		map.put("conditionCash", cashRed.getConditionCash());
		map.put("rewardCash", cashRed.getRewardCash());
		map.put("isActive", cashRed.getIsActive());
		
		List<Map<String,Object>> typeList = new ArrayList<>();
		Map<String,Object> typeMap1 = new HashMap<>();
		typeMap1.put("name", "通用");
		typeMap1.put("value", 1);
		typeMap1.put("have", 0);
		Map<String,Object> typeMap2 = new HashMap<>();
		typeMap2.put("name", "专用");
		typeMap2.put("value", 2);
		typeMap2.put("have", 0);
		Map<String,Object> typeMap3 = new HashMap<>();
		typeMap3.put("name", "注册红包");
		typeMap3.put("value", 3);
		typeMap3.put("have", 0);
		Map<String,Object> typeMap4 = new HashMap<>();
		typeMap4.put("name", "节日红包");
		typeMap4.put("value", 4);
		typeMap4.put("have", 0);
		
		switch(cashRed.getType()){
		case 1:
			typeMap1.put("have", 1);
			break;
		case 2:
			typeMap2.put("have", 1);
			break;
		case 3:
			typeMap3.put("have", 1);
			break;
		case 4:
			typeMap4.put("have", 1);
			break;
		}
		typeList.add(typeMap1);
		typeList.add(typeMap2);
		typeList.add(typeMap3);
		typeList.add(typeMap4);
		
		map.put("typeList", typeList);
		map.put("useCashCondition", cashRed.getUseCash()==null?"":cashRed.getUseCash().split(",")[0]);
		map.put("useCashResult", cashRed.getUseCash()==null?"":cashRed.getUseCash().split(",")[1]);
		map.put("useCash", cashRed.getUseCash()==null?"":"满"+cashRed.getUseCash().split(",")[0]+"可使用"+cashRed.getUseCash().split(",")[1]);
		
		map.put("startTime", cashRed.getStartTime()==null?"":sdf.format(cashRed.getStartTime()));
		map.put("endingTime", cashRed.getEndingTime()==null?"":sdf.format(cashRed.getEndingTime()));
		map.put("createTime", cashRed.getCreateTime()==null?"":sdf.format(cashRed.getCreateTime()));
		map.put("productId", cashRed.getProductId()==null?"":cashRed.getProductId());
		map.put("limitCount", cashRed.getLimitCount()==null?"":cashRed.getLimitCount());
		
		//List<CashRedMall> crmList = cashRedMallService.selectByCashRedId(cashRed.getId());
		//List<CashRedSeller> crsList = cashRedSellerService.selectByCashRedId(cashRed.getId());
		//List<CashRedCategory> crcList = cashRedCategoryService.selectByCashRedId(cashRed.getId());
		//List<CashRedProduct> crpList = cashRedProductService.selectByCashRedId(cashRed.getId());
		
		//商城列表
		List<Category> categoryAllList = new ArrayList<>();
		
        Map<String,Object> mallPara = new HashMap<>();
		List<Mall> mallList = mallService.selectByPara(mallPara);
        List<Map<String,Object>> mList = new ArrayList<>();
        if(mallList.size()>0){
        	for (Mall m: mallList)
            {	
            	Map<String,Object> mallMap = new HashMap<>();
            	
            	Map<String,Object> searchPara = new HashMap<>();
            	searchPara.put("mallUuid", m.getMallUuid());
            	searchPara.put("cashRedId", cashRed.getId());
            	CashRedMall cm = cashRedMallService.selectByCashRedIdAndMallUuid(searchPara);
            	if(cm != null){
            		mallMap.put("hava", 1);
            		
            		//增加该商城下的所有分类
            		Map<String,Object> categoryMap = new HashMap<>();
    				categoryMap.put("mallUuid", cm.getMallUuid());
    				List<Category> categoryList = categoryMallService.selectCategoryByPara(categoryMap);
    				categoryAllList.addAll(categoryList);
            	}else{
            		mallMap.put("hava", 0);
            	}
            	
            	mallMap.put("mallUuid", m.getMallUuid());
            	mallMap.put("mallName", m.getMallName());
            	mList.add(mallMap);
            }
        	System.out.println("categoryAllList---------"+categoryAllList.size());
        	json.put("mallList", mList);
        }
        
        //供应商列表
        Map<String,Object> productSellerPara = new HashMap<>();
		List<ProductSeller> productSellerAllList = productSellerService.selectAllByPara(productSellerPara);
		List<Map<String,Object>> psList = new ArrayList<>();
		if(productSellerAllList.size()>0){
			for (ProductSeller productSeller: productSellerAllList)
	        {	
	        	Map<String,Object> psMap = new HashMap<>();
	        	
	        	Map<String,Object> searchPara = new HashMap<>();
	        	searchPara.put("productSellerId", productSeller.getId());
	        	searchPara.put("cashRedId", cashRed.getId());
	        	CashRedSeller cs = cashRedSellerService.selectByCashRedIdAndProductSellerId(searchPara);
	        	if(cs != null){
	        		psMap.put("hava", 1);
	        	}else{
	        		psMap.put("hava", 0);
	        	}
	        	psMap.put("id", productSeller.getId());
	        	psMap.put("name", productSeller.getName());
	        	psList.add(psMap);
	        }
			json.put("productSellerList", psList);
		}
		
		//二级分类列表
		List<Product> productAllList = new ArrayList<>();
		
		List<Map<String,Object>> cList = new ArrayList<>();
		if(categoryAllList.size()>0){
			for (Category category: categoryAllList)
	        {	
	        	Map<String,Object> cMap = new HashMap<>();
	        	
	        	Map<String,Object> searchPara = new HashMap<>();
	        	searchPara.put("categoryId", category.getId());
	        	searchPara.put("cashRedId", cashRed.getId());
	        	CashRedCategory cc = cashRedCategoryService.selectByCashRedIdAndCategoryId(searchPara);
	        	if(cc != null){
	        		cMap.put("hava", 1);
	        		
	        		//增加该分类下的所有商品
            		Map<String,Object> categoryMap = new HashMap<>();
    				categoryMap.put("categoryId", cc.getCategoryId());
    				List<Product> productList = productService.selectByPara(categoryMap);
    				productAllList.addAll(productList);
	        	}else{
	        		cMap.put("hava", 0);
	        	}
	        	cMap.put("id", category.getId());
	        	cMap.put("name", category.getName());
	        	cList.add(cMap);
	        }
			System.out.println(productAllList.size());
			json.put("categoryList", cList);
		}
		
		//商品列表
		List<Map<String,Object>> pList = new ArrayList<>();
		if(productAllList.size()>0){
			for (Product product: productAllList)
	        {	
	        	Map<String,Object> pMap = new HashMap<>();
	        	
	        	Map<String,Object> searchPara = new HashMap<>();
	        	searchPara.put("productId", product.getId());
	        	searchPara.put("cashRedId", cashRed.getId());
	        	CashRedProduct cp = cashRedProductService.selectByCashRedIdAndProductId(searchPara);
	        	if(cp != null){
	        		pMap.put("hava", 1);
	        	}else{
	        		pMap.put("hava", 0);
	        	}
	        	pMap.put("id", product.getId());
	        	pMap.put("name", product.getName());
	        	pList.add(pMap);
	        }
			json.put("productList", pList);
		}
		
    	json.put("result", "0");
		json.put("description", "获取红包信息成功");
		json.put("cashRed", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "cashRed/create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		net.sf.json.JSONArray saleMallArea = bodyInfo.getJSONArray("saleMallArea");
		net.sf.json.JSONArray saleSellerArea = bodyInfo.getJSONArray("saleSellerArea");
		net.sf.json.JSONArray saleCategoryArea = bodyInfo.getJSONArray("saleCategoryArea");
		net.sf.json.JSONArray saleProductArea = bodyInfo.getJSONArray("saleProductArea");
		
		int right = 1;
		if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}else if(bodyInfo.get("conditionCash") == null || "".equals(bodyInfo.get("conditionCash"))){
			right = 0;
		}else if(bodyInfo.get("rewardCash") == null || "".equals(bodyInfo.get("rewardCash"))){
			right = 0;
		}else if(bodyInfo.get("type") == null || "".equals(bodyInfo.get("type"))){
			right = 0;
		}else if(bodyInfo.get("startTime") == null || "".equals(bodyInfo.get("startTime"))){
			right = 0;
		}else if(bodyInfo.get("endingTime") == null || "".equals(bodyInfo.get("endingTime"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CashRed cashRed = new CashRed();
		cashRed.setName(bodyInfo.getString("name"));
		cashRed.setConditionCash(Float.valueOf(bodyInfo.getString("conditionCash")));
		cashRed.setRewardCash(Float.valueOf(bodyInfo.getString("rewardCash")));
		cashRed.setIsActive((byte)1);
		
		if(bodyInfo.get("useCashCondition") != null && !"".equals(bodyInfo.get("useCashCondition")) 
				&& bodyInfo.get("useCashResult") != null && !"".equals(bodyInfo.get("useCashResult"))){
			cashRed.setUseCash(bodyInfo.getString("useCashCondition")+","+bodyInfo.getString("useCashResult"));
		}
		
		cashRed.setType(Byte.valueOf(bodyInfo.getString("type")));
		cashRed.setStartTime(sdf.parse(bodyInfo.getString("startTime")));
		cashRed.setEndingTime(sdf.parse(bodyInfo.getString("endingTime")));
		cashRed.setCreateTime(new Date());
		
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))){
			cashRed.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
		}
		
		if(bodyInfo.get("limitCount") != null && !"".equals(bodyInfo.get("limitCount"))){
			cashRed.setLimitCount(Integer.valueOf(bodyInfo.getString("limitCount")));
		}
		
		Integer insert = cashRedService.insertSelective(cashRed);
		if(insert != 1){
			System.out.println("---新增红包失败---");
			json.put("result", "0");
			json.put("description", "新增红包失败");
			return buildReqJsonObject(json);
		}else{
			Date now = new Date();
			//红包使用范围
			if(saleMallArea.size()>0 && saleMallArea != null){
				for(int i=0;i<saleMallArea.size();i++){
					if(saleMallArea.get(i) != null){
						CashRedMall crm = new CashRedMall();
						crm.setCashRedId(cashRed.getId());
						crm.setMallUuid(saleMallArea.get(i).toString());
						crm.setStatus((byte)1);
						crm.setCreateTime(now);
						cashRedMallService.insertSelective(crm);
					}
				}
			}
			if(saleSellerArea.size()>0 && saleSellerArea != null){
				for(int i=0;i<saleSellerArea.size();i++){
					if(saleSellerArea.get(i) != null){
						CashRedSeller crs = new CashRedSeller();
						crs.setCashRedId(cashRed.getId());
						crs.setProductSellerId(Integer.valueOf(saleSellerArea.get(i).toString()));
						crs.setStatus((byte)1);
						crs.setCreateTime(now);
						cashRedSellerService.insertSelective(crs);
					}
				}
			}
			if(saleCategoryArea.size()>0 && saleCategoryArea != null){
				for(int i=0;i<saleCategoryArea.size();i++){
					if(saleCategoryArea.get(i) != null){
						CashRedCategory crc = new CashRedCategory();
						crc.setCashRedId(cashRed.getId());
						crc.setCategoryId(Integer.valueOf(saleCategoryArea.get(i).toString()));
						crc.setStatus((byte)1);
						crc.setCreateTime(now);
						cashRedCategoryService.insertSelective(crc);
					}
				}
			}
			if(saleProductArea.size()>0 && saleProductArea != null){
				for(int i=0;i<saleProductArea.size();i++){
					if(saleProductArea.get(i) != null){
						CashRedProduct crp = new CashRedProduct();
						crp.setCashRedId(cashRed.getId());
						crp.setProductId(Integer.valueOf(saleProductArea.get(i).toString()));
						crp.setStatus((byte)1);
						crp.setCreateTime(now);
						cashRedProductService.insertSelective(crp);
					}
				}
			}
		}
		
    	json.put("result", "0");
		json.put("description", "新增红包成功");
		json.put("cashRedId", cashRed.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "cashRed/modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		net.sf.json.JSONArray saleMallArea = bodyInfo.getJSONArray("saleMallArea");
		net.sf.json.JSONArray saleSellerArea = bodyInfo.getJSONArray("saleSellerArea");
		net.sf.json.JSONArray saleCategoryArea = bodyInfo.getJSONArray("saleCategoryArea");
		net.sf.json.JSONArray saleProductArea = bodyInfo.getJSONArray("saleProductArea");
		
		int right = 1;
		if(bodyInfo.get("cashRedId") == null || "".equals(bodyInfo.get("cashRedId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CashRed cashRed = cashRedService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("cashRedId")));
		if(cashRed == null){
			json.put("result", "1");
			json.put("description", "红包信息不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))){
			cashRed.setName(bodyInfo.getString("name"));
		}
		if(bodyInfo.get("conditionCash") != null && !"".equals(bodyInfo.get("conditionCash"))){
			cashRed.setConditionCash(Float.valueOf(bodyInfo.getString("conditionCash")));
		}
		if(bodyInfo.get("rewardCash") != null && !"".equals(bodyInfo.get("rewardCash"))){
			cashRed.setRewardCash(Float.valueOf(bodyInfo.getString("rewardCash")));
		}
		if(bodyInfo.get("startTime") != null && !"".equals(bodyInfo.get("startTime"))){
			cashRed.setStartTime(sdf.parse(bodyInfo.getString("startTime")));
		}
		if(bodyInfo.get("endingTime") != null && !"".equals(bodyInfo.get("endingTime"))){
			cashRed.setEndingTime(sdf.parse(bodyInfo.getString("endingTime")));
		}
		if(bodyInfo.get("isActive") != null && !"".equals(bodyInfo.get("isActive"))){
			cashRed.setIsActive(Byte.valueOf(bodyInfo.getString("isActive")));
		}
		if(bodyInfo.get("useCashCondition") != null && !"".equals(bodyInfo.get("useCashCondition")) 
				&& bodyInfo.get("useCashResult") != null && !"".equals(bodyInfo.get("useCashResult"))){
			cashRed.setUseCash(bodyInfo.getString("useCashCondition")+","+bodyInfo.getString("useCashResult"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))){
			cashRed.setType(Byte.valueOf(bodyInfo.getString("type")));
		}
		
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))){
			cashRed.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
		}
		
		if(bodyInfo.get("limitCount") != null && !"".equals(bodyInfo.get("limitCount"))){
			cashRed.setLimitCount(Integer.valueOf(bodyInfo.getString("limitCount")));
		}
		
		Integer insert = cashRedService.updateByPrimaryKeySelective(cashRed);
		if(insert != 1){
			System.out.println("---更新红包失败---");
			json.put("result", "0");
			json.put("description", "更新红包失败");
			return buildReqJsonObject(json);
		}else{
			Date now = new Date();
			//红包使用范围
			List<CashRedMall> crmList = cashRedMallService.selectByCashRedId(cashRed.getId());
			for(CashRedMall crm:crmList){
				cashRedMallService.deleteByPrimaryKey(crm.getId());
			}
			List<CashRedSeller> crsList = cashRedSellerService.selectByCashRedId(cashRed.getId());
			for(CashRedSeller crs:crsList){
				cashRedSellerService.deleteByPrimaryKey(crs.getId());
			}
			List<CashRedCategory> crcList = cashRedCategoryService.selectByCashRedId(cashRed.getId());
			for(CashRedCategory crc:crcList){
				cashRedCategoryService.deleteByPrimaryKey(crc.getId());
			}
			List<CashRedProduct> crpList = cashRedProductService.selectByCashRedId(cashRed.getId());
			for(CashRedProduct crp:crpList){
				cashRedProductService.deleteByPrimaryKey(crp.getId());
			}
			
			if(saleMallArea.size()>0 && saleMallArea != null){
				for(int i=0;i<saleMallArea.size();i++){
					if(saleMallArea.get(i) != null){
						CashRedMall crm = new CashRedMall();
						crm.setCashRedId(cashRed.getId());
						crm.setMallUuid(saleMallArea.get(i).toString());
						crm.setStatus((byte)1);
						crm.setCreateTime(now);
						cashRedMallService.insertSelective(crm);
					}
				}
			}
			if(saleSellerArea.size()>0 && saleSellerArea != null){
				for(int i=0;i<saleSellerArea.size();i++){
					if(saleSellerArea.get(i) != null){
						CashRedSeller crs = new CashRedSeller();
						crs.setCashRedId(cashRed.getId());
						crs.setProductSellerId(Integer.valueOf(saleSellerArea.get(i).toString()));
						crs.setStatus((byte)1);
						crs.setCreateTime(now);
						cashRedSellerService.insertSelective(crs);
					}
				}
			}
			if(saleCategoryArea.size()>0 && saleCategoryArea != null){
				for(int i=0;i<saleCategoryArea.size();i++){
					if(saleCategoryArea.get(i) != null){
						CashRedCategory crc = new CashRedCategory();
						crc.setCashRedId(cashRed.getId());
						crc.setCategoryId(Integer.valueOf(saleCategoryArea.get(i).toString()));
						crc.setStatus((byte)1);
						crc.setCreateTime(now);
						cashRedCategoryService.insertSelective(crc);
					}
				}
			}
			if(saleProductArea.size()>0 && saleProductArea != null){
				for(int i=0;i<saleProductArea.size();i++){
					if(saleProductArea.get(i) != null){
						CashRedProduct crp = new CashRedProduct();
						crp.setCashRedId(cashRed.getId());
						crp.setProductId(Integer.valueOf(saleProductArea.get(i).toString()));
						crp.setStatus((byte)1);
						crp.setCreateTime(now);
						cashRedProductService.insertSelective(crp);
					}
				}
			}
		}
		
    	json.put("result", "0");
		json.put("description", "更新红包成功");
		json.put("cashRedId", cashRed.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "cashRed/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("cashRedId") == null || "".equals(bodyInfo.get("cashRedId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		CashRed cashRed = cashRedService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("cashRedId")));
		if(cashRed == null){
			json.put("result", "1");
			json.put("description", "红包信息不存在");
			return buildReqJsonObject(json);
		}
		
		Integer insert = cashRedService.deleteByPrimaryKey(cashRed.getId());
		if(insert != 1){
			System.out.println("---删除红包失败---");
			json.put("result", "0");
			json.put("description", "删除红包失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除红包成功");
		return buildReqJsonObject(json);
	}
}
